package com.atguigu.ssm.demo.controller;

import com.atguigu.ssm.demo.entity.GourdBaby;
import com.atguigu.ssm.demo.service.api.GbSerive;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author : zzf
 * @version : 1.0
 * @date : 2021/1/13
 */

@Controller
@RequestMapping("gb")
public class GbController {
    @Autowired
    private GbSerive gbSerive;

    @RequestMapping("list/{page}")
    public String showGbList(@PathVariable("page") Integer page,Model model) {
        System.out.println("page111 = " + page);
        List<GourdBaby> list = gbSerive.getAll(page);
        PageInfo<GourdBaby> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public String removeGb(@PathVariable("id") Integer id) {
        gbSerive.remove(id);
        return "redirect:/gb/list/1";
    }

    @RequestMapping("add")
    public String addGb(Model model) {
        List<String> colorList = gbSerive.getColorList();
        model.addAttribute("colorList",colorList);
        return "add";
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public String saveGb(GourdBaby gourdBaby) {
        gbSerive.saveGb(gourdBaby);
        return "redirect:/gb/list/1";
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public String toUpdatePage(@PathVariable("id") Integer id,Model model) {
        GourdBaby gourdBaby = gbSerive.getGb(id);
        List<String> colorList = gbSerive.getColorList();
        model.addAttribute("gourdBaby",gourdBaby);
        model.addAttribute("colorList",colorList);
        return "edit";
    }

    @RequestMapping(value = "",method = RequestMethod.PUT)
    public String updateGb(GourdBaby gourdBaby) {
        gbSerive.updateGb(gourdBaby);
        return "redirect:/gb/list/1";
    }
}
